package com.androidaz.scanner;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;

import com.google.zxing.Result;
import com.google.zxing.client.android.CaptureActivity;

/**
 * Glavna aktivnost aplikacije
 * @author Vojislav Djukic
 *
 */
public class ScannerActivity extends CaptureActivity 
{
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

	//poziva se prilikom uspesnog dekodiranja
	@Override 
	public void handleDecode(Result rawResult, Bitmap barcode) 
	{	
		//pronadji telefonski broj
		final String phoneNumber=findPhoneNumber(rawResult.getText());
		if(phoneNumber==null){
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			    @Override
			    public void onClick(DialogInterface dialog, int which) {}
			};

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Prepoznati tekst: "+rawResult.getText())
				.setPositiveButton("Ok", dialogClickListener).show();
		}else{
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			    @Override
			    public void onClick(DialogInterface dialog, int which) {
			        switch (which){
			        case DialogInterface.BUTTON_POSITIVE:
			        	Intent callIntent = new Intent(Intent.ACTION_CALL);
			        	callIntent.setData(Uri.parse("tel:"+phoneNumber));
			        	startActivity(callIntent);
			            break;
			        case DialogInterface.BUTTON_NEGATIVE:
			            break;
			        }
			    }
			};

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Broj telefona:"+phoneNumber+". Pozovi?")
				.setPositiveButton("Da", dialogClickListener)
			    .setNegativeButton("Ne", dialogClickListener).show();
			
		}
		//postavi Delay radi sledeceg prepoznavanja kako bi se memorija pocistila
		restartPreviewAfterDelay(1000);
		
		
	}

	/**
	 * Pretpostavlja se da je telefonski broj niz brojeva koji moze pocinjati + znakom i sadrzi bar 7 cifara
	 * U ulaznom nizu karaktera broj telefona (njegove cifre) mogu biti podeljeni znakovima ;:\| .-/\n
	 * @param text
	 * 		ulazni tekst
	 * @return
	 * 		prepoznati broj telefona ili null
	 * */
	protected String findPhoneNumber(String text){		
		String[] partitions=text.split("[;:\\| \\.\\\\\\-/\\n]+");
		
		ArrayList<String> potentialStrings=new ArrayList<String>();
		
		for(int i=0;i<partitions.length;i++){
			if(partitions[i].matches("^[\\+0-9]*")){
				String potentialString=partitions[i];
				i++;
				while(i<partitions.length){
					if(partitions[i].matches("^[\\+0-9]*")){
						potentialString+=partitions[i];
						i++;
					}
					else{
						break;
					}
				}
				potentialStrings.add(potentialString);
			}
		}
		
		for(String phoneNumber:potentialStrings){
			if(phoneNumber.length()>6 && !phoneNumber.substring(1).contains("+"))
				return phoneNumber;
		}
		return null;
	}

}